import * as homeService from '../../services/home/homeService';
import * as personService from '../../services/home/personService';
import moment from 'moment';

export default {
  namespace: 'personModel',
  state: {
    loading: false,
    userCounts: null,
    redisInfos: [],
    mqInfos: [],
    scheduleIndex: 0,
    scheduleTotal: 0,
    scheduleData: null,
    scheduleList: null,
    serviceInvokes: null,
    userStatistics: [],
    selectedYear: null,
    userRelateVo: null,
    userInfoVo: null,
  },
  reducers: {
    updateState(state, { payload }) {
      return { ...state, ...payload }
    }
  },
  effects: {
    *init({ payload: params }, { select, call, put }) {
      yield put({ type: 'getUserCount', payload: {}});
      yield put({ type: 'getMessages', payload: {}});
      yield put({ type: 'getRedisInfos', payload: {}});
      yield put({ type: 'queryServiceInvokes', payload: {}});
      yield put({ type: 'getSchedules', payload: {}});
      yield put({ type: 'onSelectYear', payload: {year: moment(new Date()).format("YYYY")}});
      yield put({ type: 'updateState', payload: { loading: false }});

      const time = moment(new Date()).format("HH:mm:ss");

      // Redis数据初始化
      const redisInfos = [
        {id: "11111", time, value: "0", type: "keyValue"},
        {id: "22222", time, value: "0", type: "memory"}
      ];

      // MQ数据初始化
      const mqInfos = [
        {id: "11111", time, value: "0", type: "message"},
        {id: "22222", time, value: "0", type: "exchange"},
        {id: "33333", time, value: "0", type: "queue"},
        {id: "44444", time, value: "0", type: "channel"}
      ];

      let userJson = sessionStorage.getItem("userInfo");
      let userInfoVo = JSON.parse(userJson);
      yield put({ type: 'updateState', payload: { redisInfos, mqInfos, userInfoVo }});
    },

    *getUserCount({ payload: params }, { select, call, put }) {
      try {
        const datas = yield call(homeService.getUserCount, {});
        if (datas.code == 200) {
          yield put({ type: 'updateState', payload: { userCounts: datas.data }});
        }
      } catch (e) {
        console.log("homeModel getUserCount error: " + e);
      }
    },

    *getMessages({ payload: params }, { select, call, put }) {
      try {
        const res = yield call(homeService.getMessages, params);
        if (res.code == 200) {
          let {mqInfos} = yield select(state => state.homeModel);
          mqInfos = [...mqInfos, ...res.data];
          yield put({ type: "updateState", payload: { mqInfos }});
        }
      } catch (e) {
        console.log("homeModel getMessages error: " + e);
      }
    },

    *getRedisInfos({ payload: params }, { select, call, put }) {
      try {
        const res = yield call(homeService.getRedisInfos, params);
        if (res.code == 200) {
          let {redisInfos} = yield select(state => state.homeModel);
          redisInfos = [...redisInfos, ...res.data];
          yield put({ type: "updateState", payload: { redisInfos }});
        }
      } catch (e) {
        console.log("homeModel getRedisInfos error: " + e);
      }
    },

    *queryServiceInvokes({ payload: params }, { select, call, put }) {
      try {
        const res = yield call(homeService.queryServiceInvokes, params);
        if (res.code == 200) {
          yield put({ type: "updateState", payload: { serviceInvokes: res.data }});
        }
      } catch (e) {
        console.log("homeModel getMessages error: " + e);
      }
    },

    *getCorrelateByUser({ payload: params }, { select, call, put }) {
      let userJson = sessionStorage.getItem("userInfo");
      let userObj = JSON.parse(userJson);
      params.tenantId = "83b6578e717b408cb09bdc284f0ad192";
      params.appId = "14c9addf834a40e09c20f8a01c90f766";
      params.userId = userObj.userId;
      const datas = yield call(personService.getCorrelateByUser, params);
      if (datas.code == 200) {
        yield put({ type: 'updateState', payload: { userRelateVo: datas.data }});
      }
    },

    *getAuthorizeByUser({ payload: params }, { select, call, put }) {
      let userJson = sessionStorage.getItem("userInfo");
      let userObj = JSON.parse(userJson);
      params.tenantId = "83b6578e717b408cb09bdc284f0ad192";
      params.appId = "14c9addf834a40e09c20f8a01c90f766";
      params.userId = userObj.userId;
      const datas = yield call(personService.getAuthorizeByUser, params);
      if (datas.code == 200) {
        yield put({ type: 'updateState', payload: { userRelateVo: datas.data }});
      }
    },

    *onSelectYear({payload: params}, {select, call, put}) {
      const res = yield call(homeService.statisticOnlineByYear, params);
      if (res.code == 200) {
        yield put({type: "updateState", payload: {userStatistics: res.data}});
      }
    },

    *getSchedules({ payload: params }, { select, call, put }) {
      const allScheduleList = [
        {id: "11111", theme: "技术选型会议", themeColor: "red", startTime: "2020-04-05 00:00:00", endTime: "2019-11-07 00:00:00", location: "会议室2019", participant: "11111", content: "1111111111"},
        {id: "22222", theme: "小组讨论", themeColor: "blue", startTime: "2020-04-05 00:00:00", endTime: "2019-11-08 00:00:00", location: "会议室2019", participant: "22222", content: "2222222222"},
        {id: "33333", theme: "代码评审", themeColor: "green", startTime: "2020-04-05 00:00:00", endTime: "2019-11-17 00:00:00", location: "会议室2019", participant: "33333", content: "3333333333"},
        {id: "44444", theme: "技术选型会议", themeColor: "purple", startTime: "2020-04-05 00:00:00", endTime: "2019-11-17 00:00:00", location: "会议室2019", participant: "44444", content: "4444444444"},
        {id: "55555", theme: "小组讨论", themeColor: "blue", startTime: "2020-04-05 00:00:00", endTime: "2019-11-27 00:00:00", location: "会议室2019", participant: "55555", content: "5555555555"},
      ];
      // const scheduleList = allScheduleList.filter(item => moment(new Date()).format("YYYY-MM-DD") == moment(item.startTime).format("YYYY-MM-DD"));
      const scheduleList = allScheduleList.filter(item => "2020-04-05" == moment(item.startTime).format("YYYY-MM-DD"));
      yield put({ type: "updateState", payload: { scheduleData: scheduleList[0], scheduleList, scheduleTotal: scheduleList.length }});
    },

    *onClickArrow({payload: params}, {select, call, put}) {
      const {arrowType} = params;
      let {scheduleIndex, scheduleTotal, scheduleData, scheduleList} = yield select(state => state.homeModel);
      let resultData = {};
      if (arrowType == "left") {
        scheduleIndex = scheduleIndex > 0 ? scheduleIndex - 1 : scheduleTotal - 1;
      } else {
        scheduleIndex = scheduleIndex < (scheduleTotal - 1) ? scheduleIndex + 1 : 0;
      }
      scheduleData = scheduleList[scheduleIndex];
      yield put({ type: "updateState", payload: { scheduleIndex, scheduleData }});
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      const pathname = window.location.pathname;
      console.log("homeModel subscriptions setup pathname => ", pathname);
      if (pathname === "/home/person") {
        if (window._TOKEN_ || sessionStorage.token) {
          dispatch({ type: 'init', payload: {}});
        }
      }
    },
  },
};
